import 'package:wch/utils/log_utils.dart';

class DigitalUtils {
  static bool chinaPhoneNumber(String input) {
    if (input.isEmpty) return false;
    //手机正则验证
    String regexPhoneNumber =
        "^((13[0-9])|(15[^4])|(166)|(17[0-9])|(18[0-9])|(19[0-9])|(147,145))\\d{8}\$";
    return RegExp(regexPhoneNumber).hasMatch(input);
  }

  static bool isValidUrl(String url) {
    final RegExp regex = RegExp(
      r"^((((H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))://)?(www.|[a-zA-Z0-9].)[a-zA-Z0-9-.]+.[a-zA-Z]{2,6}(:[0-9]{1,5})*(/($|[a-zA-Z0-9.,;?'\+&amp;%$#=~_-]+))*$",
    );
    return regex.hasMatch(url);
  }

  static bool isValidIPAddress(String ipAddress) {
    final RegExp regex = RegExp(
        r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):(?<port>[0-9]{1,5})$'
    );
    return regex.hasMatch(ipAddress);
  }

  ///1 version1 >= version2
  ///0 version1 < version2
  static int compareVersionType(String version1, String version2) {
    List<int> v1 = version1.split('.').map(int.parse).toList();
    // List<int> v2 = version2.split('.').map(int.parse).toList();
    List<int> v2 = [];
    if (version2.contains('.')) {
      v2 = version2.split('.').map(int.parse).toList();
    } else {
      for (int i = 0; i < version2.length; i ++) {
        v2.add(int.parse(version2.substring(i, i+1)));
      }
    }


    int minLength = v1.length < v2.length ? v1.length : v2.length;

    int result = 0;

    for (int i = 0; i < minLength; i++) {
      if (v1[i] < v2[i]) {
        result = -1;
        break;
      } else if (v1[i] > v2[i]) {
        result = 1;
        break;
      }
    }

    if (result == 0 && v1.length != v2.length) {
      result = v1.length < v2.length ? -1 : 1;
    }

    if (result < 0) {
      return 1;
    } else if (result > 0) {
      return 0;
    } else {
      return 1;
    }
  }

  static int byteToInt(int b) {
    if (b > 127) {
      b = -(256 - b);
    }
    return b;
  }

}